<template>
  <el-dialog title="选择原料" :visible.sync="dialogVisible" :before-close="handleClose" width="700px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="id" prop="id">
        <el-input style="width: 100px" v-model.trim="queryParams.id" placeholder="id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label-width="100px" label="原料名称" prop="name">
        <el-input style="width: 150px" v-model.trim="queryParams.name" placeholder="原料名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="nameList" ref="multipleTable">
      <el-table-column label="原料ID" align="center" width="200px" key="id" prop="id" />
      <el-table-column label="原料名称" align="center" key="name" prop="name" :show-overflow-tooltip="true" />
      <el-table-column fixed="right" align="center" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="selectItem(scope.row)" type="text" size="small">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </el-dialog>
</template>

<script>
  import {
    listMaterial
  } from "@/api/service/material"
  export default {
    props: {
      dialogCategoryId: {
        type: [String, Number],
        default: null,
      },
      dialogVisible: {
        type: Boolean,
        default: false,
      }
    },
    data() {
      return {
        loading: false,
        queryParams: {
          name: null,
          id: null,
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        nameList: [],
      };
    },
    methods: {
      selectItem(row) {
        console.log('row', row);
        
        let obj = {
          row: row,
          id: this.dialogCategoryId
        }
        this.$emit("onSubmit", obj);
        this.handleClose();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      handleClose() {
        this.$emit("onClose");
        this.resetForm("queryForm");
      },
      handleQuery() {
        this.getList();
      },
      getList() {
        this.loading = true;
        listMaterial(this.queryParams).then((res) => {
          this.nameList = res?.rows;
          this.total = res?.total;
          this.loading = false;
        }).finally(() => {
          this.loading = false;
        });
      },
    },
    watch: {
      dialogVisible(newValue, oldValue) {
        if (newValue) {
          this.getList();
        }
      },
    },
  };
</script>

<style lang="scss" scoped></style>
